'''
Created on Oct 23, 2011

@author: alonyado
'''

from CommunicationThread import CommunicationThread
import time
 
class CommunicationControler:
   
    def __init__(self):
        self.__reactorLogic = dict()
      
    def addReader(self,cameraName ,config):
        if not(config.has_key('ip') and config.has_key('port') and config.has_key('request')):
            raise NameError('Configuration Doesn''t have minimum')
        shallowCfg = dict()
        shallowCfg['ip'] = config['ip']
        shallowCfg['port'] = config['port']
        shallowCfg['request'] = config['request']

        if (self.__reactorLogic.has_key(cameraName)):
            raise NameError('Camera with same name already exists!')
        
        foundH = None
        foundCamName = None
        for cName, h in self.__reactorLogic.iteritems():
            interstingCfg = dict()
            interstingCfg['ip'] = h.config['ip']
            interstingCfg['port'] = h.config['port']
            interstingCfg['request'] = h.config['request']
            if interstingCfg == shallowCfg:
                foundH = h
                foundCamName = cName
                print 'WARN: Camera "%s" and Camera "%s" with same config exist'%(foundCamName, cameraName)
        if (foundH == None):
            self.__reactorLogic[cameraName] = CommunicationThread(cameraName, config)
        else:
            self.__reactorLogic[cameraName] = CommunicationThread(cameraName, config)
            #raise NameError('camera "%s" has same config'%foundCamName)

    def removeReader(self, cameraName):
        if (self.__reactorLogic.has_key(cameraName)):
            self.__reactorLogic[cameraName].stop()
            self.__reactorLogic.pop(cameraName)
        else:
            raise NameError('Camera doesn''t exists'%cameraName)

    def getCameras(self):
        return self.__reactorLogic.keys()
    
    def activateCamera(self, cameraName):
        if (self.__reactorLogic.has_key(cameraName)):
            if not(self.__reactorLogic[cameraName].isAlive()):
                self.__reactorLogic[cameraName].start()
            else:
                print 'WARN: Camera "%s" already active'%cameraName
        else:
            cameLs = ','.join(self.__reactorLogic.keys())
            raise NameError('Camera "%s" doesn''t exists. please select one from "%s"'%(cameraName, cameLs))

    def deActivateCamera(self, cameraName):
        if (self.__reactorLogic.has_key(cameraName)):
            self.__reactorLogic[cameraName].stop()
            cameraConfig = self.__reactorLogic[cameraName].config
            self.__reactorLogic[cameraName] = CommunicationThread(cameraName, cameraConfig)
        else:
            cameLs = ','.join(self.__reactorLogic.keys())
            raise NameError('Camera "%s" doesn''t exists. please select one from "%s"'%(cameraName, cameLs))
        
    def IsActive(self, cameraName):
        if not(self.__reactorLogic.has_key(cameraName)):
            cameLs = ','.join(self.__reactorLogic.keys())
            raise NameError('Camera "%s" doesn''t exists. please select one from "%s"'%(cameraName, cameLs))

        return self.__reactorLogic[cameraName].running
        

    def StopAll(self):
        camLs = self.getCameras()
        for c in camLs:
            if (self.IsActive(c)):
                print 'WARN! Camera "%s" was still active and stopping all!'%c
            self.__reactorLogic[c].stop()
        self.__reactorLogic.clear()
       
